package com.example.cny.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cny.R;
import com.example.cny.adapter.BluetoothListAdapter;
import com.example.cny.adapter.FunctionListAdapter;
import com.example.cny.dialog.BluetoothDialog;
import com.example.cny.presenter.HomePresenter;
import com.example.mylibrary.base.BaseFragment;
import com.example.mylibrary.db.bean.FunctionBean;
import com.example.mylibrary.di.component.BaseComponent;
import com.example.mylibrary.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment<HomePresenter> {
    @BindView(R.id.home_recyclerView)
    RecyclerView recyclerView;

    private PopupWindow popupWindow;
    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected void setupFragmentComponent(BaseComponent baseComponent) {

    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initData() {
        initRecyclerView();
    }

    private void initRecyclerView(){
        List<FunctionBean> list = new ArrayList<>();
        list.add(new FunctionBean(R.mipmap.green_bg,R.mipmap.home_data_icon,"练习数据","你有新的练习数据！"));
        list.add(new FunctionBean(R.mipmap.yellow_bg,R.mipmap.home_try_icon,"开始练习","业精于勤荒于嬉！"));
        list.add(new FunctionBean(R.mipmap.pink_bg,R.mipmap.home_fighting_icon,"排行榜","有人刷新了新的排行榜！"));
        list.add(new FunctionBean(R.mipmap.zi_bg,R.mipmap.home_pk_icon,"在线PK","有人向你发起挑战了！"));
        FunctionListAdapter adapter = new FunctionListAdapter(R.layout.layout_function_item,list);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void showNoBluetoothDialog(){
        final BluetoothDialog confirmDialog = new BluetoothDialog(getActivity());
        confirmDialog.show();
        confirmDialog.setClicklistener(new BluetoothDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {}

            @Override
            public void doSetting() {

            }
        });
    }

    private void showLowPowerDialog(){
        // // 获取自定义布局文件pop.xml的视图
        View customView = getLayoutInflater().inflate(R.layout.popup_low_power,
                null, false);
        // 创建PopupWindow实例,200,150分别是宽度和高度
        popupWindow = new PopupWindow(customView, ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT );
        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 自定义view添加触摸事件
        customView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
        popupWindow.showAtLocation(recyclerView, Gravity.TOP, DisplayUtils.dp2px(getContext(),16),DisplayUtils.dp2px(getContext(),6));
    }
}
